package com.ceuer.pet.dao.impl;

import com.ceuer.pet.dao.UserDao;
import com.ceuer.pet.entity.User;
import com.ceuer.pet.util.DateUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
	
	@Override
	public User login(String mobile, String password) {
		User user = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			String sql = "select id,role,mobile,name,regtime,logintime,status,enable from user where status=1 and mobile=? and password=?";
			conn = this.getConn();
			pst = conn.prepareStatement(sql);
			pst.setString(1, mobile);
			pst.setString(2, password);
			rs = pst.executeQuery();
			while (rs.next()) {
				user = new User();
				//数据库中user表的字段和User对象的属性一一对应的过程 就是 映射关系
				user.setId(rs.getInt("id"));
				user.setRole(rs.getInt("role"));
				user.setMobile(rs.getString("mobile"));
				user.setName(rs.getString("name"));
				user.setRegtime(rs.getString("regtime"));
				user.setLogintime(rs.getString("logintime"));
				user.setStatus(rs.getInt("status"));
				user.setEnable(rs.getInt("enable"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeAll(conn, pst, rs);
		}
		return user;
	}
	
	@Override
	public User selectByMobile(String mobile) {
		/*
			数据库的连接对象
			pst sql的预编译对象
			结果集对象 rs
			关闭上述对象
		 */
		User user = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			conn = this.getConn();
			String sql = "select id,role,mobile,name,password,regtime,logintime,status,enable from user where mobile=?";
			pst = conn.prepareStatement(sql);
			pst.setString(1, mobile);
			rs = pst.executeQuery();//执行查询
			while (rs.next()) {
				user = new User();
				//数据库中user表的字段和User对象的属性一一对应的过程 就是 映射关系
				user.setId(rs.getInt("id"));
				user.setRole(rs.getInt("role"));
				user.setMobile(rs.getString("mobile"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setRegtime(rs.getString("regtime"));
				user.setLogintime(rs.getString("logintime"));
				user.setStatus(rs.getInt("status"));
				user.setEnable(rs.getInt("enable"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//不管是否有错误，都需要在最后关闭资源
			this.closeAll(conn, pst, rs);
		}
		return user;
	}
	
	@Override
	public boolean reg(User user) {
		List<Object> param = new ArrayList<>();
		param.add(user.getPassword());
		param.add(user.getName());
		param.add(DateUtil.now());
		param.add(user.getMobile());
		String sql="";
		if(user.getStatus()!=null && user.getStatus()==2){//之前账号注销的
			sql="update user set password=?,name=?,regtime=?,status=1 where id=(select id from (select id from user where mobile=?) temp)";
		}else{
			sql = "insert into user(role,status,enable,password,name,regtime,mobile) values(2,1,1,?,?,?,?)";
		}
		int num = this.executeUpdate(sql, param);
		return num == 1;
	}
	
	@Override
	public boolean update(User user) {
		List<Object> param = new ArrayList<>();
		//String sql="update user set password=?,status=? where id=?";//修改密码的sql
		String sql = "update user set ";//修改密码的sql
		if (user.getRole() != null) {
			sql += "role=?,";
			param.add(user.getRole());
		}
		if (user.getPassword() != null && user.getPassword().trim().length() > 0) {
			sql += "password=?,";
			param.add(user.getPassword());
		}
		if (user.getName() != null && user.getName().trim().length() > 0) {
			sql += "name=?,";
			param.add(user.getName());
		}
		if (user.getRegtime() != null) {
			sql += "regtime=?,";
			param.add(user.getRegtime());
		}
		if (user.getLogintime() != null) {
			sql += "logintime=?,";
			param.add(user.getLogintime());
		}
		if (user.getStatus() != null) {
			sql += "status=?,";
			param.add(user.getStatus());
		}
		if (user.getEnable() != null) {
			sql += "enable=?,";
			param.add(user.getEnable());
		}
		if (sql.substring(sql.length() - 1).equals(",")) {
			sql = sql.substring(0, sql.length() - 1);
			sql += " where id=?";
			param.add(user.getId());
			int num = this.executeUpdate(sql, param);
			return num == 1;
		} else {
			return false;
		}
	}
	
	@Override
	public boolean updatePassword(Integer id, String password) {
		List<Object> param = new ArrayList<>();
		param.add(password);
		param.add(id);
		String sql = "update user set password=? where id=?";//修改密码的sql
		int num = this.executeUpdate(sql, param);
		//num=1成功 num=0失败
		return num == 1;
	}
	
	@Override
	public boolean logoff(Integer id) {
		List<Object> param = new ArrayList<>();
		param.add(id);
		String sql = "update user set status=2 where id=?";//注销账户的sql
		int num = this.executeUpdate(sql, param);
		//num=1成功 num=0失败
		return num == 1;
	}
	
	@Override
	public boolean enable(Integer id) {
		List<Object> param = new ArrayList<>();
		param.add(id);
		String sql = "update user set enable=1 where id=?";//启用账户的sql
		int num = this.executeUpdate(sql, param);
		//num=1成功 num=0失败
		return num == 1;
	}
	
	@Override
	public boolean disable(Integer id) {
		List<Object> param = new ArrayList<>();
		param.add(id);
		String sql = "update user set enable=2 where id=?";//禁用账户的sql
		int num = this.executeUpdate(sql, param);
		//num=1成功 num=0失败
		return num == 1;
	}
}